package tests;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import conference.TextParse;

/** Tests the TextParse class
 * @author Shawn Paul Smith - ssmith35 */
public class TextParseTest {
	private List<String[]> my_listArray;
	private TextParse my_parse;

	// The contents of the User file
	private static final String USER_TXT =
			"Matt Minch, ConfA, Program Chair";
	// The folder in which we run our tests.
	private static final String TEST_FOLDER =
			"EZConferenceTest";
	// The path for the User.Txt
	private static final String USER_PATH = TEST_FOLDER
			+ File.separator + "user.txt";

	@Before
	public void setup() throws IOException {
		// Create a fresh folder
		File testFolder = new File(TEST_FOLDER);
		if (testFolder.exists()) {
			testFolder.delete();
		}
		testFolder.mkdir();

		// Create the user.txt file
		File userTxt = new File(USER_PATH);
		if (userTxt.exists()) {
			userTxt.delete();
		}
		FileWriter fw = new FileWriter(userTxt);
		fw.write(USER_TXT);
		fw.flush();
		fw.close();

		// create parser
		my_parse = new TextParse(new File("config.txt"));

		// setup list of arrays
		my_listArray = new ArrayList<String[]>();
		String[] string = new String[] { "Matt Minch",
				"ConfA", "Program Chair" };
		my_listArray.add(string);
	}

	
	@Test(expected = FileNotFoundException.class)
	public void testInvalidFileLocation()
			throws IOException {
		TextParse fakeParse = new TextParse(new File(
				"DoesntExist"));
		fail();
	}

	@Test
	public void testReadUserFile()
			throws IOException {
		ArrayList<String[]> compare =
				(ArrayList<String[]>) my_parse
				.readUserFile();
		for (int i = 0; i < compare.size(); i++) {
			for (int j = 0; j < 3; j++) {
				assertEquals("readUserFile didnt work" + i,
						my_listArray.get(i)[j],
						compare.get(i)[j]);
			}
		}
	}
	
	@Test(expected = FileNotFoundException.class)
	public void testReaderMissingUserFile()
			throws IOException {
		//read config file
		//Delete the user file
		File usertxt = new File(USER_PATH);
		assertTrue(usertxt.delete());
		//attempt to read the user file
		my_parse.readUserFile();
		fail();
	}

}
